﻿//--------------------------------------------------------------------- 

//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY 

//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 

//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 

//PARTICULAR PURPOSE. 

//---------------------------------------------------------------------

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PWDepositary
{
    public static class ControlExtension
    {
        public static void ShowBorder(this Control control, bool value)
        {

            int currentStyle = (int) GetWindowLong(control.Handle, (-16));

            if (value)
            {
                currentStyle |= WS_BORDER;
            }
            else
            {
                currentStyle = (int)GetWindowLong(control.Handle, (-16));
                currentStyle &= ~ WS_BORDER;

            }
            SetWindowLong(control.Handle, (-16), currentStyle);
            SetWindowPos(control.Handle, IntPtr.Zero, 0, 0, control.Width, control.Height, SWP_NOSIZE |
                                                                                           SWP_NOACTIVATE | SWP_NOMOVE | SWP_FRAMECHANGED);
            
        }
       

        #region P/Invoke declarations

        [DllImport("coredll.dll", SetLastError = true)]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("coredll.dll", SetLastError = true)]
        static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("coredll.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
                                        int Y, int cx, int cy, uint uFlags);
        
        static int WS_BORDER = 0x00800000;

        public static uint           
            SWP_NOSIZE = 0x0001,
            SWP_NOMOVE = 0x0002,
            SWP_NOZORDER = 0x0004,
            SWP_NOREDRAW = 0x0008,
            SWP_NOACTIVATE = 0x0010,
            SWP_FRAMECHANGED = 0x0020;

        #endregion

    }
}


